package app.server.project.main.service.controller;

import app.server.project.core.constants.Def;
import app.server.project.core.exception.SysError;
import app.server.project.core.http.RespModel;
import app.server.project.core.httpauth.UnAuth;
import app.server.project.core.token.TokenCache;
import app.server.project.core.util.AssertUtil;
import app.server.project.main.service.dto.cms.CoinDto;
import app.server.project.main.service.dto.race.EarnReq;
import app.server.project.main.service.dto.race.GetUserRaceDetailReq;
import app.server.project.main.service.dto.race.GetUserRaceDetailResp;
import app.server.project.main.service.dto.race.RaceInfoDto;
import app.server.project.main.service.service.race.RaceCfgService;
import app.server.project.main.service.service.race.RaceService;
import app.server.project.main.service.service.race.UserRaceDetailService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 赛道信息
 */
@AllArgsConstructor
@RestController
@RequestMapping("/raceInfo")
public class RaceInfoController {

    private RaceService raceService;
    private UserRaceDetailService userRaceDetailService;
    private RaceCfgService raceCfgService;

    /**
     * 赛道信息配置列表
     *
     * @param userId
     * @return
     */
    @PostMapping("/getRaceInfo")
    public RespModel<List<RaceInfoDto>> getRaceInfo(@RequestHeader(Def.UserId) long userId) {
        return RespModel.success(this.raceService.getRaceInfo(userId));
    }

    /**
     * 点击赛道,赚金币
     *
     * @param userId
     * @return
     */
    @PostMapping("/earnCoin")
    public RespModel earnCoin(@RequestHeader(Def.UserId) long userId, @RequestBody EarnReq req) {
        this.raceService.earnCoin(req, userId);
        return RespModel.success();
    }

    /**
     * 拉取赛道数值变化明细
     *
     * @param userId
     * @return
     */
    @PostMapping("/getAllUserRaceDetail")
    public RespModel<GetUserRaceDetailResp> getAllUserRaceDetail(@RequestHeader(Def.UserId) long userId, @RequestBody GetUserRaceDetailReq req) {
        return RespModel.success(this.userRaceDetailService.getAllUserRaceDetail(userId, req));
    }

    /**
     * 上传客户端代码
     *
     * @param req
     */
    @UnAuth
    @PostMapping("/uploadLogo/{userId}/{token}/{raceId}")
    public RespModel uploadLogo(@RequestParam("Fdata") MultipartFile files, @PathVariable int raceId, @PathVariable long userId, @PathVariable String token) {
        AssertUtil.isTrue(TokenCache.chkToken(userId, token), SysError.ErrorToken);
        this.raceCfgService.uploadLogo(files, raceId);
        return RespModel.success();
    }

    /**
     * 上传客户端代码
     *
     * @param req
     */
    @UnAuth
    @PostMapping("/uploadNama/{userId}/{token}/{raceId}")
    public RespModel uploadNama(@RequestParam("Fdata") MultipartFile files, @PathVariable int raceId, @PathVariable long userId, @PathVariable String token) {
        AssertUtil.isTrue(TokenCache.chkToken(userId, token), SysError.ErrorToken);
        this.raceCfgService.uploadNama(files, raceId);
        return RespModel.success();
    }


    @PostMapping("/addCoin")
    public RespModel addCoin(@RequestBody CoinDto addCoinReq) {
        this.raceCfgService.addCoin(addCoinReq);
        return RespModel.success();
    }

    @PostMapping("/consumeCoin")
    public RespModel consumeCoin(@RequestBody CoinDto addCoinReq) {
        this.raceCfgService.consumeCoin(addCoinReq);
        return RespModel.success();
    }
}
